IF exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SaveQuestionnaireAnswer]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[SaveQuestionnaireAnswer]
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO 
/******************************************************************  
* Name:   SaveQuestionnaireAnswer  
* Purpose:  Saves a Protocol Version with the values passed in. If  
*     the Id parameter passed in is 0, it will insert a new  
8     record, otherwise it updates the existing one.  
*  
* PARAMETERS  
* Name          Description       
* -------------      -------------------------------------------  
*  @id       Identity column of this QA  
*  @questionnaireVersionId  QuestionnaireVersionId this answer relates to  
*  @name      The name of this QA- typically the name of the Questionnaire,  
        unless it's a multiple (ex. Drugs), othewise it'll have a unique name  
*  @required     Flag if this QA is required or not- if so, can't be deleted   
*  @editable     Flag is this QA is able to be edited - for AMendment  
*  @isClone  
*  @actionId  
*  @updateModifiedDate    Flag to indicate whether or not the date modified should be updated.  
*  @highlight     Flag to indicate whether or not the questionnaire answer is some importance  
******************************************************************/  
  
CREATE Procedure [dbo].[SaveQuestionnaireAnswer] (  
  @id    INTEGER = 0,  
  @questionnaireVersionId  INTEGER ,  
  @name   Varchar(256) ,  
  @required  BIT,  
  @editable  BIT,  
  @externalId  INT,  
  @isClone  bit,  
  @actionId  int,  
  @updateModifiedDate bit,  
  @highlight  bit,
  @lastModifiedByAdmPersonId int,
  @status Varchar(32) = null  
)  
AS  
BEGIN  
  
 SET NOCOUNT ON  
   
 -- if it's zero, then insert  
 IF @id = 0   
  BEGIN  
    
  INSERT INTO QuestionnaireAnswer (   
        QuestionnaireVersionId,  
        [Name],  
        Required,  
        Editable,  
        ExternalId,  
        IsClone,  
        ActionTypeId,  
        LastModifiedDate,  
        Highlight,
		LastModifiedByAdmPersonId,
		Status
		)  
    Values  
       ( @questionnaireVersionId,  
        @name,  
        @required,  
        @editable,  
        @externalId,  
        @isClone,  
        @actionId,  
        case @updateModifiedDate when 1 then GetDate() else null end,  
        @highlight, 
		@lastModifiedByAdmPersonId,
		@status
		)  
  
  IF @@ERROR <> 0 GOTO ERROR_HANDLER  
    
  SELECT SCOPE_IDENTITY()  
  
  END  
    
   
 -- else update ony the Name and Script of the existing one.  
 ELSE  
  UPDATE QuestionnaireAnswer SET QuestionnaireVersionId = @questionnaireVersionId,  
       [Name] = @name,  
       Required = @required,  
       Editable = @editable,  
       LastModifiedDate = GETDATE(),  
       ExternalId = @externalId,  
       IsClone = @isClone,  
       ActionTypeId = @actionId,  
       Highlight = @highlight,
	   LastModifiedByAdmPersonId  = @lastModifiedByAdmPersonId,
	   Status = @status
        
   WHERE Id = @id  
   
  IF @@ERROR <> 0 GOTO ERROR_HANDLER  
  
 SELECT @id   
  
 ERROR_HANDLER:  
  RAISERROR('Unable to Save QuestionnaireAnswer', 16, 1)  
  RETURN  
    
END  
  